package dao.imp;

import dao.DaoLogic;
import javabean.Admin;
import javabean.House;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import tools.Dbutils;

import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

import static tools.Dbutils.getSqlSession;
import static tools.Dbutils.logs;

public class DaoLogicImp {
    Scanner scanner = new Scanner(System.in);
    /**
     * 1.新增房源
     * @param
     */
    public void addDao() {
        System.out.println("---请输入你要添加的房屋信息---");
        System.out.println("请输入姓名：");
        String name = scanner.next();
        System.out.println("请输入电话：");
        String tel = scanner.next();
        System.out.println("请输入地址：");
        String address = scanner.next();
        System.out.println("请输入月租");
        String rent = scanner.next();
        System.out.println("请输入状态");
        String state = scanner.next();
        House house = new House();
        house.setName(name);
        house.setTel(tel);
        house.setAddress(address);
        house.setRent(rent);
        house.setState(state);
        System.out.println(house);
        logs();
        SqlSession sqlSession = getSqlSession();
        DaoLogic mapper = sqlSession.getMapper(DaoLogic.class);
        Integer integer = mapper.addDao(house);//理解成把什么（一个对象JavaBean或者hashmap）加进去
        System.out.println("---成功插入"+integer+"条房屋信息---");
        sqlSession.commit();
    }
    /**
     *2.查找房屋信息
     * @param
     * @param
     * @return
     */

    public House findDao(int id) {//这里方法的参数是与接口方法中的参数无关的，各是各的
        logs();
        SqlSession sqlSession = getSqlSession();
        DaoLogic mapper = sqlSession.getMapper(DaoLogic.class);
        House house = mapper.findDao(id);
        if(house==null){
            return null;
        }
        System.out.println("该房屋的信息是："+house);
        return house;
    }

    /**
     * 3.删除房屋信息
     * @param
     * @param
     * @return
     */

    public Integer delDao(int id) {
        logs();
        SqlSession sqlSession = getSqlSession();
        DaoLogic mapper = sqlSession.getMapper(DaoLogic.class);
        Integer integer = mapper.delDao(id);
        sqlSession.commit();
        return integer;
    }

    /**
     * 4.修改房屋信息
     * @param
     * @param
     */

    public void updateDao(int id) {
        logs();
        SqlSession sqlSession = getSqlSession();
        DaoLogic mapper = sqlSession.getMapper(DaoLogic.class);
        House house = mapper.findDao(id);
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();

//        scanner.nextLine();
        objectObjectHashMap.put("id1",id);
//我这里给了两个id键，一个是用id找到这条信息，一个是更改的id，都放在hashmap里面，这也是我是用hashmap而不使用JavaBean封装数据的原因
        System.out.println("请输入新的房屋编号：");
//        scanner.nextLine();
        String id1 = scanner.nextLine();
        if(id1.equals("")){
            objectObjectHashMap.put("id",id);
        }else{
            objectObjectHashMap.put("id",id1);
        }

//        scanner.nextLine();
        System.out.println("请输入新的姓名：");
        String name = scanner.nextLine();
        if(name.equals("")){
            objectObjectHashMap.put("name",house.getName());
        }else {
            objectObjectHashMap.put("name",name);
        }

        System.out.println("请输入新的电话：");
        String tel = scanner.nextLine();
        if(tel.equals("")){
            objectObjectHashMap.put("tel",house.getTel());
        }else {
            objectObjectHashMap.put("tel",tel);
        }

        System.out.println("请输入新的房屋地址：");
        String address = scanner.nextLine();
        if(address.equals("")){
            objectObjectHashMap.put("address",house.getAddress());
        }else {
            objectObjectHashMap.put("address",address);
        }

        System.out.println("请输入新的房屋月租：");
        String rent = scanner.nextLine();
        if(rent.equals("")){
            objectObjectHashMap.put("rent",house.getRent());
        }else {
            objectObjectHashMap.put("rent",rent);
        }

        System.out.println("请输入新的房屋状态：");
        String state = scanner.nextLine();
        if(state.equals("")){
            objectObjectHashMap.put("state",house.getState());
        }else {
            objectObjectHashMap.put("state",state);
        }
        Integer update = mapper.updateDao(objectObjectHashMap);
        sqlSession.commit();
            if(update>0){
                System.out.println("---成功修改"+update+"条房源信息---");
            }else{
                System.out.println("修改失败！");
            }
    }

    /**
     * 5.房屋列表
     * @param
     */

    @Test
    public void listDao() {
        logs();
        SqlSession sqlSession = getSqlSession();
        List<House> houses = sqlSession.selectList("HouseDate.findAll");
        System.out.println("编号"+"\t"+"姓名"+"\t"+"电话"+"\t"+"地址"+"\t"+"月租"+"\t"+"状态"+"\t");
        for (int i = 0; i < houses.size(); i++) {
            System.out.println(houses.get(i).getId()+"\t"+houses.get(i).getName()+"\t"
                    +houses.get(i).getTel()+"\t"+houses.get(i).getAddress()+"\t"+houses.get(i).getRent()
                    +"\t"+houses.get(i).getState()+"\t");
        }
    }
    public Admin adminDao(String user,String password){
        Dbutils.logs();
        SqlSession sqlSession = getSqlSession();
        Admin admin1 = new Admin();
        admin1.setUser(user);
        admin1.setPassword(password);
        Admin admin = sqlSession.selectOne("HouseDate.isAdmin", admin1);
        return admin;
    }
//    从ui到dao：传参；从dao到ui，return
}
